package com.swfgui.managers
{
	import GameMain.utility.EffectUtility;
	
	import com.swfgui.blit.BlitClip;
	import com.swfgui.core.Application;
	import com.swfgui.managers.resources.ResourceManager;
	import com.swfgui.queue.MethodQueueElement;
	import com.swfgui.utils.display.DisplayUtil;
	import com.swfgui.utils.display.Geom;
	import com.swfgui.utils.time.STimer;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;

	public class EffectManager
	{
		private static var effectList:Array = [];
			
		
		/**
		 * 超时自动隐藏
		 */
		public var timeOut:Number = 0;
		private var timeOutId:uint;
		
		/**
		 * 播放特效
		 * @param effect url或者MC
		 * @param loop 循环次数，如果为-1，则永远循环
		 * @param onComplete
		 * @param onCompleteArgs
		 * @param parent 默认在特效层播放
		 * @param hcenter 水平居中间距，如果为NaN，则不处理
		 * @param vcenter 垂直居中间距，如果为NaN，则不处理
		 * @return 特效实例
		 */
		public static function addEffect(effect:Object, 
										 fps:int = 30,
										 loop:int = 1,
										 onComplete:Function=null, 
										 onCompleteArgs:Array=null, 
										 parent:DisplayObjectContainer=null,
										 hcenter:Number=0, 
										 vcenter:Number=0):EffectManager
		{
			var eff:EffectManager = new EffectManager();
			effectList.push(eff);
			eff.addEffect(effect, fps, loop, onComplete, onCompleteArgs, parent, hcenter, vcenter);			
			return eff;
		}
		
		/**
		 * 删除特效
		 * @param effect url或者MC，如果是url，则删除所有url加载生成的特效
		 */
		public static function removeEffect(effect:Object):void
		{
			for each(var eff:EffectManager in effectList)
			{
				if(eff.effect == effect)
				{
					eff.removeEffect();
				}
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  实例
		//
		//--------------------------------------------------------------------------
		
		public var effect:Object;
		public var mc:MovieClip;
		public var bc:BlitClip;
		public var url:String;
		public var onComplete:MethodQueueElement;
		public var parent:DisplayObjectContainer;
		public var hcenter:Number;
		public var vcenter:Number;
		public var loop:int;
		
		public function EffectManager()
		{
		}
		
		
		public function addEffect(effect:Object, 
								  fps:int = 30,
								  loop:int = 1,
								  onComplete:Function=null, 
								  onCompleteArgs:Array=null, 
								  parent:DisplayObjectContainer=null,
								  hcenter:Number=0, 
								  vcenter:Number=0):void
		{			
			bc = new BlitClip();
			bc.ctrlChild = false;
			bc.fps = fps;
			
			this.effect = effect;
			this.loop = loop;
			this.onComplete = new MethodQueueElement(onComplete, onCompleteArgs);
			this.parent = parent ? parent : Application.instance.effectLayer;
			this.hcenter = hcenter;
			this.vcenter = vcenter;
			
			if(effect is String)
			{
				url = effect as String;
				ResourceManager.loadOne(url, null, function():void
				{
					mc = ResourceManager.getMovieClip(url);	
					startEffect();
				});
			}
			else
			{
				mc = effect as MovieClip;
				startEffect();
			}
		}
		
		public function startEffect():void
		{
			if(mc)
			{
				DisplayUtil.setMouseEnabled(mc, false);
				bc.fromMovieClipNotDraw(mc);
				parent.addChild(mc);
				Application.instance.stage.addEventListener(Event.RESIZE, onResize);				
				onResize();
				bc.playFromTo(1, bc.totalFrames, removeEffect, null, loop);
				
				if(timeOut > 0)
				{
					timeOutId = setTimeout(stopEffect, timeOut);
				}
			}
			else
			{
				removeEffect();
			}
		}
		
		public function stopEffect():void
		{
			if(timeOut > 0)
			{
				clearTimeout(timeOutId);
			}
			if(bc)
			{
				bc.stop();
			}
			if(mc)
			{
				DisplayUtil.safeRemoveChild(mc);
			}
			Application.instance.stage.removeEventListener(Event.RESIZE, onResize);
		}
		
		protected function onResize(event:Event=null):void
		{
			var rect:Rectangle = Geom.getPxRect(mc, mc);
			if(!isNaN(hcenter))
			{
				mc.x = (parent.width - rect.width) * 0.5 + hcenter;
			}
			if(!isNaN(vcenter))
			{
				mc.y = (parent.height - rect.height) * 0.5 + vcenter;
			}
			
			mc.x -= rect.x;
			mc.y -= rect.y;
		}
		
		public function removeEffect():void
		{
			this.onComplete.call();
			this.onComplete.clear();
			if(bc)
			{
				bc.dispose();
			}
			if(mc)
			{
				DisplayUtil.safeRemoveChild(mc);
				mc = null;
			}
			parent = null;
			Application.instance.stage.removeEventListener(Event.RESIZE, onResize);
		}
	}
}